﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;

namespace PPAP.Web.Supplier
{
    public partial class PlantEdit : PageBase
    {
        PlantBO bo = new PlantBO();
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Model.Entity.SUPPLIER su = new SupplierBO().GetModel(CurrentUser.UnitName);
                if (su != null)
                {
                    txtSupplierNo.Text = su.SUPPLIERCODE;
                    txtSupplierName.Text = su.SUPPLIERNAME;
                }
                if (Request.QueryString["PID"] != null)
                {
                    LoadCon(Request.QueryString["PID"].ToString());
                }
                else
                {
                    Session["SupplierInfo"] = CurrentUser.UnitName;
                }
            }
        }


        protected override void SkinControl()
        {
            //base.SkinControl();
        }

       

        private void LoadCon(string id)
        {
            PPAP.Model.Plant p = bo.GetModel(int.Parse(Request.QueryString["PID"].ToString()));
            txtAddress.Text = p.ADDRESS;
            txtMainBusiness.Text = p.MAINBUSINESS;
            txtPlantCode.Text = p.PLANTCODE;
            txtPlantName.Text = p.PLANTNAME;
            txtTelephone.Text = p.TELEPHONE;
            Button1.Text = "Save";
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if ( txtPlantName.Text.Trim() == "" || txtSupplierNo.Text.Trim() == "")
            {
                AlertMessage("The text frame not is empty");
                return;
            }
            if (Session["SupplierInfo"] == null)
            {
                AlertMessage("No SupplierInfo");
            }
            else
            {
                int num = 0; PPAP.Model.Plant p = new Model.Plant();
                if (Request.QueryString["PID"] != null)
                    p = bo.GetModel(int.Parse(Request.QueryString["PID"].ToString()));
                p.ADDRESS = txtAddress.Text;
                p.MAINBUSINESS = txtMainBusiness.Text;
                p.PLANTCODE = txtPlantCode.Text;
                p.PLANTNAME = txtPlantName.Text;
                p.SUPPILERNO = CurrentUser.Code;
                p.TELEPHONE = txtTelephone.Text;
                p.SUPPLIERID = int.Parse(Session["SupplierInfo"].ToString());
                p.Supplieradmin = CurrentUser.Id;
                if (Button1.Text == "Add")
                {
                    num = bo.Add(p);
                    if (num > 0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "Add Plant:" + p.PLANTNAME);
                        AlertReflashOpener("Save success");
                    }
                }
                else if (Button1.Text == "Save")
                {
                    num = bo.Update(p);
                    if (num > 0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "Edit Plant:" + p.PLANTNAME);
                        Button1.Text = "Add";
                        AlertReflashOpener("Save success");
                    }
                }
                txtAddress.Text = "";
                txtMainBusiness.Text = "";
                txtPlantCode.Text = "";
                txtPlantName.Text = "";
                txtSupplierNo.Text = "";
                txtTelephone.Text = "";
            }
        }

    }
}